﻿CREATE PROCEDURE [dbo].[UpdateUser]
@UserID INT, @UserName NVARCHAR (50), @FullName VARCHAR (50), @UserTypeID INT, @RoleID INT, @ExpectedHours SMALLINT, @IsActive BIT, @StartDate DATETIME, @IsAdministrative BIT, @PTOHoursRolledIntoCurrentYear DECIMAL (6, 2)
AS
IF Not Exists (SELECT UserName FROM Users WHERE UserName=@UserName AND UserID<>@UserID)
   BEGIN
	UPDATE 
		Users
		SET UserName=@UserName,
			FullName = @FullName,
			UserTypeID = @UserTypeID,
		    RoleID = @RoleID,
			ExpectedHours = @ExpectedHours,
			IsActive = @IsActive,
			StartDate = @StartDate,
			IsAdministrative = @IsAdministrative,
			PTOHoursRolledIntoCurrentYear = @PTOHoursRolledIntoCurrentYear
	WHERE 
		UserID=@UserID

	IF (@@Error<>0) GOTO ErrorHandler
	SELECT 
		1 AS retval
   END
 
ELSE
	SELECT
		-2 AS retval

IF @@Error<>0
	GOTO ErrorHandler
ELSE
   BEGIN
 
	RETURN (0)
   END

ErrorHandler:
   BEGIN
 
	RETURN (1)
   END

